$(function() {
	layerInfo();


	$('#basicInfo_form')
			.validate(
					{
						errorElement : 'div',
						errorClass : 'help-block',
						focusInvalid : true,
						ignore : "",
						rules : {
							name : {
								required : true,
								maxlength : 100
							}
						},

						messages : {
							name : {
								required : "<span style='color: red;'>请输入名称！</span>",
								maxlength : "<span style='color: red;'>最大长度不能超过100个字符！</span>"
							}
							
							// gis_area : {
							// required : "<span style='color:
							// red;'>请输入一些描述！</span>",
							// maxlength : "<span style='color:
							// red;'>最大长度不能超过500个字符！</span>"
							// },
							
						},

						highlight : function(e) {
							$(e).closest('.form-group').removeClass('has-info')
									.addClass('has-error');
							basicInfo.html(basicInfo.text() + warnHTML);
						},

						success : function(e) {
							$(e).closest('.form-group')
									.removeClass('has-error').addClass(
											'has-info');
							// $(e).remove();
						}
					});

	$("#basicInfo_btn").click(function() {
		basicInfo_add();
	});
	$("#add_administratordivision").citySelect({
		nodata : "none",
		required : false
	});
	getBasicInfos();
});

function layerInfo() {
	dojo.require("esri.map");
	$("#location_gis_layer").empty();
	var layer = [];
	var mapserver = $("#location_gis_mapserver").val();

	$.ajax({
		url : "findmapserver",
		dataType : "json",
		type : "POST",
		data : {mapserver:mapserver},
		async : false,
		error : function(error) {
			console.log(error.responseText);

			$.gritter.add({
				title : '服务器错误',
				text : '对不起，服务器错误，请联系系统管理员！',
				class_name : 'gritter-error'
			});

		},
		success : function(strValue) {
			if (strValue.length != 0) {

				$("#location_gis_layer").empty();

				for ( var i in strValue) {
					var option = "<option value=" + strValue[i] + ">"
							+ strValue[i] + "</option>";
					$("#location_gis_layer").append(option);
				}

			}else{
				$("#location_gis_mapserver").empty();
				$("#location_gis_layer").empty();
				$("#location_gis_mapserver").append("<option value=' '>请选择</option>");
				$("#location_gis_layer").append("<option value=' '>请选择</option>");
			}
		}
	});

}

function gismap() {

	
	$("#location_gis_map").empty();
	var filepath = $("#location_gis_layer").val();
	var map;
	var layer;
	
	dojo.require("dijit.layout.BorderContainer");
	dojo.require("dijit.layout.ContentPane");
	dojo.require("esri.map");
	function init() {

		map = new esri.Map("location_gis_map",{
			basemap : "streets",
			slider : true
		});
		
		//var basemap = new esri.layers.ArcGISTiledMapServiceLayer("http://cache1.arcgisonline.cn/ArcGIS/rest/services/ChinaOnlineCommunity/MapServer");
        //map.addLayer(basemap);
		var point = new esri.geometry.Point(longitude,
				latitude);
		map.centerAndZoom(point, scale);
		layer = new esri.layers.ArcGISDynamicMapServiceLayer(filepath);

		if (layer.loaded) {
			buildLayerList(layer);
		} else {
			dojo.connect(layer, "onLoad", buildLayerList);
		}

	}
	function buildLayerList(layer) {
		var layerinfos = layer.layerInfos;
		var visible = [];
		var visiblelayer = [];
		if (layerinfos != null && layerinfos.length > 0) {
			
			for (var i = 0, j = layerinfos.length; i < j; i++) {
			var info = layerinfos[i];
			info.defaultVisibility == true;
//			var option = "<option value=" + info.name + ">" + info.name
//					+ "</option>";
//			$("#location_gis_layer").append(option);
			visible.push(info.id);
			}
			
			
			if (visible.length == 0) {
				visible.push(-1);
			}
			layer.setVisibleLayers(visible);
		}
		map.addLayer(layer);
	}
	dojo.ready(init);
}

function basicInfo_add() {
	var option = {
		dataType : 'json',
		data : {
			wetlandParkId : wetlandParkId
		},
		success : function(value) {
			if (value == true) {
				basicInfo.html(basicInfo.text() + successHTML);
				$.gritter.add({
					title : '确认消息',
					text : '恭喜您,信息添加成功！',
					class_name : 'gritter-success'
				});
			} else {
				basicInfo.html(basicInfo.text() + warnHTML);
				$.gritter.add({
					title : '确认消息',
					text : '对不起,信息添加失败！',
					class_name : 'gritter-error'
				});
			}
		},
		error : function() {
			basicInfo.html(basicInfo.text() + warnHTML);
			$.gritter.add({
				title : '服务器错误',
				text : '对不起，服务器错误，请联系系统管理员！',
				class_name : 'gritter-error'
			});
		}
	};
	$("#basicInfo_form").ajaxForm(option).submit();

}
function getBasicInfos(){
	var id =$("#wetlandParkId").val();
	$.ajax({
		url : "findbyid",
		dataType : "json",
		type : "POST",
		data : {id:id},
		async : false,
		error : function(error) {
			console.log(error.responseText);

			$.gritter.add({
				title : '服务器错误',
				text : '对不起，服务器错误，请联系系统管理员！',
				class_name : 'gritter-error'
			});

		},
		success : function(strValue) {
			if (strValue != null) {
				$("#scale").find("option[value='"+strValue.wlin_scale+"']").attr("selected",true);
//				$("#location_gis_layer").find("option[value='"+strValue.wlin_location_gis_layer+"']").attr("selected",true);
//				$("#prov").val(strValue.wlin_province);
				
//				$("#city").val(strValue.wlin_city);
			
//				$("#dist").val(strValue.wlin_district);
			}
		}
	});
}

